//
//  TTSizeGuideView.m
//  TaoTuo
//
//  Created by admin on 2020/1/4.
//  Copyright © 2020 admin. All rights reserved.
//

#import "TTSizeGuideView.h"
#import <WebKit/WebKit.h>
#import "TTSizeGuidePlayerViewController.h"

@interface TTSizeGuideView ()<WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic , strong) WKWebViewConfiguration *configuration;


@end

@implementation TTSizeGuideView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self webView];
        
    }
    return self;
}


- (void)setupUrl:(NSURL *)url
{
    [MBProgressHUD showLoadingAnimationOnView:self];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}

#pragma mark - WKNavigationDelegate
// 加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [MBProgressHUD dismissAnimationOnView:self];
}

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    if ([message.name isEqualToString:@"playJiezhiVideo"] || [message.name isEqualToString:@"playShouzhuoVideo"]) {
        
        TTSizeGuidePlayerViewController *playerVC = [[TTSizeGuidePlayerViewController alloc] initWithVideoUrlString:message.body];
        [[LLRouteTool getCurrentNavigationController] pushViewController:playerVC animated:YES];
        
    }
}

- (UIView *)listView
{
    return self;
}


- (WKWebView *)webView
{
    if (!_webView) {
        YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
        self.configuration = [[WKWebViewConfiguration alloc] init];
        self.configuration.userContentController = [[WKUserContentController alloc] init];
        [self.configuration.userContentController addScriptMessageHandler:proxy name:@"playJiezhiVideo"];
        [self.configuration.userContentController addScriptMessageHandler:proxy name:@"playShouzhuoVideo"];
        _webView = [[WKWebView alloc] initWithFrame:self.bounds configuration:self.configuration];
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        [self addSubview:_webView];
        
    }
    return _webView;
}


@end
